package work.vcloud.baseinfo.controller;


import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
@Scope("prototype")
public class RelateController extends BaseController {

	@Autowired
	OpenDao openDao;

	/**
	 * 删除用户关联表信息
	 */
	public void deleteUserRelation(Map<String, Object> map) throws Exception {
		try {
			map.put("sqlMapId", "user.recove");
			openDao.delete(map);
			
			map.put("sqlMapId", "role.recove");
			openDao.delete(map);
			
			map.put("sqlMapId", "organization.recoveUser");
			openDao.delete(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除用户表的教师
	 */
	@RequestMapping("api/teacher.recove")
	public OutputVo teacherRecove() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("form", "baseinfo.sc_user_teacher");
			map.put("type", "1");
			
			deleteUserRelation(map);

			map.put("sqlMapId", "teacher.recove");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	/**
	 * 删除用户表的学生
	 */
	@RequestMapping("api/student.recove")
	public OutputVo studentRecove() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("form", "baseinfo.sc_user_student");
			map.put("type", "2");
			
			deleteUserRelation(map);
			
			map.put("sqlMapId", "student.recove");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	
	
	/**
	 * 删除单位关联表信息
	 */
	public void deleteUnitRelation(Map<String, Object> map) throws Exception {
		try {
			map.put("sqlMapId", "app.recove");
			openDao.delete(map);
			
			map.put("sqlMapId", "organization.recoveUnit");
			openDao.delete(map);
			
			map.put("sqlMapId", "class.recove");
			openDao.delete(map);
			
			map.put("sqlMapId", "unit_grade.recove");
			openDao.delete(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除单位表的主管单位
	 */
	@RequestMapping("api/center.recove")
	public OutputVo centerRecove() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("form", "baseinfo.sc_unit_center");
			map.put("type", "0");
			
			deleteUnitRelation(map);

			map.put("sqlMapId", "unit.recoveCenter");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	/**
	 * 删除单位表的学校
	 */
	@RequestMapping("api/school.recove")
	public OutputVo schoolRecove() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("form", "baseinfo.sc_unit_school");
			map.put("type", "1");
			
			deleteUnitRelation(map);
			
			map.put("sqlMapId", "unit.recoveSchool");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
	/**
	 * 删除单位表的分校
	 */
	@RequestMapping("api/attach.recove")
	public OutputVo attachRecove() {
		try {

			Map<String, Object> map = getParameterMap();
			map.remove("page");
			
			map.put("form", "baseinfo.sc_unit_school_attach");
			map.put("type", "2");
			
			deleteUnitRelation(map);
			
			map.put("sqlMapId", "unit.recoveAttach");
			openDao.delete(map);
			
			return output("0000");
		} catch (Exception e) {
			e.printStackTrace();
			return output("9999");
		}
	}
}
